package com.ccx.coachsite.versionupdate;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ccx.coachsite.R;


public class SoftUpdateDialog extends Activity implements OnClickListener {
	
	private final static String TAG = "SoftUpdateDialog";
	String soft_url = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog_update_soft);
		Bundle bundle = this.getIntent().getExtras();
		TextView contentView = (TextView) findViewById(R.id.update_content);

		this.findViewById(R.id.btn_update).setOnClickListener(this);
		this.findViewById(R.id.btn_cancel).setOnClickListener(this);
		if (bundle != null && bundle.containsKey("soft_url")) {
			String versionName = SoftVersionUtils.getVersionName(this, this.getPackageName());
			String newVer = bundle.getString("new_version");
			soft_url = bundle.getString("soft_url");
			String content = bundle.getString("content");
			contentView.setText(getString(R.string.soft_update_notify_ccx, newVer, content));
		}
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
		mNotificationManager.cancel(R.string.main_bottom_app_name);
		//U.createHideInputMethod(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btn_update:
			/*
			if(!SUtil.isZhLanguage()){
				String search = "market://details?id=com.nd.android.pandahome2";
				Intent intent = new Intent(
						android.content.Intent.ACTION_VIEW);
				intent.setData(Uri.parse(search));
				startMarketActivity(this,intent);
				return;
			}
			*/
			int firstIdx = soft_url.lastIndexOf("/");
			int lastIdx = soft_url.lastIndexOf(".apk");
			if (firstIdx == -1 || lastIdx == -1) {
				showDownloadFailTip();
				return;
			}
			String fileName = soft_url.substring(firstIdx + 1, lastIdx);
			Intent intent = new Intent(this, WaitForDownloadDialog.class);
			intent.putExtra("URL", soft_url);
			intent.putExtra("NAME", fileName);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_cancel:
			finish();
			break;
		}
	}
	
	/**
	 * 启动Market
	 */
	public static void startMarketActivity( Context context, Intent intentMarket ) {
		try{
			context.startActivity( intentMarket );
		} catch( ActivityNotFoundException ex ) {
			new AlertDialog.Builder( context ).setTitle( R.string.common_tip).setMessage( context.getResources().getText( R.string.no_market_title ) )
				.setPositiveButton( R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
					public void onClick( DialogInterface dialog, int id ) {}
				}).create().show();
		} catch( Exception e ) {
			Log.e( TAG, "startMarketActivity Exception: " + e.getMessage() );
		}
	}

	public void showDownloadFailTip() {
		Toast.makeText(this, R.string.hit_network_problem, Toast.LENGTH_SHORT).show();
	}
	
}
